package com.lingCheng.avg.service.impl;

import com.lingCheng.avg.common.ErrorData;
import com.lingCheng.avg.entity.AgvErrorInfo;
import com.lingCheng.avg.entity.MemItem;
import com.lingCheng.avg.entity.SrmInfo;
import com.lingCheng.avg.mapper.MemItemMapper;
import com.lingCheng.avg.service.MemItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MemItemServiceImpl implements MemItemService {
    @Autowired
    private MemItemMapper memItemMapper;
    @Override
    public List<ErrorData> findErrorInfo() {
        List<MemItem> errorInfo = memItemMapper.findErrorInfo();
        List<ErrorData> errorDataList = new ArrayList<>();
        for (MemItem memItem : errorInfo) {
            ErrorData errorData = new ErrorData();
            if(Integer.parseInt(memItem.getCode())<=0){

                continue;
            }
            if (memItem.getName().equals("PLC_ALL_ERROR_C_01") || memItem.getName().equals("PLC_ALL_ERROR_C_02") || memItem.getName().equals("PLC_ALL_ERROR_C_03") || memItem.getName().equals("PLC_ALL_ERROR_C_04") || memItem.getName().equals("PLC_ALL_ERROR_C_05") || memItem.getName().equals("PLC_ALL_ERROR_C_06")) {
                MemItem errmsg = memItemMapper.findERRMSG(memItem.getCode());
                String nameWithoutPrefix = memItem.getName().replace("PLC_ALL_ERROR_C_", "");
                errorData.setDeviceId(memItem.getName());
                errorData.setPosition(memItem.getPosition());
                errorData.setDescription(nameWithoutPrefix + "号" + errmsg.getDescription());
            } else {
                MemItem errmsg = memItemMapper.findERRMSG2(memItem.getCode());
                String nameWithoutPrefix = memItem.getName().replace("PLC_ERROR_C_", "");
                errorData.setDeviceId(memItem.getName());
                errorData.setPosition(memItem.getPosition());
                errorData.setDescription(nameWithoutPrefix + "号" + errmsg.getDescription());
            }
            errorData.setTime(memItem.getTime());
            errorDataList.add(errorData);
        }

        List<AgvErrorInfo> agvErrorList = memItemMapper.getAgvErrorInfo();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (AgvErrorInfo agvError: agvErrorList ) {
            ErrorData errorData = new ErrorData();
//            errorData.setDeviceId(agvError.getAmrName());
//            errorData.setPosition(agvError.getPos());
//            errorData.setDescription(agvError.getWarnName());
            errorData.setTime(simpleDateFormat.format(agvError.getUpdateTime()));
            errorDataList.add(errorData);
        }

        return errorDataList;
    }

    @Override
    public List<Map<String,Object>> getSrmInfo(String deviceId){
        List<Map<String,Object>> listMap = new LinkedList<>();
        Map<String,Object> taskCountMap = new HashMap<>();
        Map<String,Object> statusMap = new HashMap<>();
        Map<String,Object> taskNoMap = new HashMap<>();
        Map<String,Object> taskTypeMap = new HashMap<>();
        Map<String,Object> startLocateMap = new HashMap<>();
        Map<String,Object> endLocateMap = new HashMap<>();
        Map<String,Object> errorCodeMap = new HashMap<>();
        taskCountMap.put("label","剩余任务数量");
        statusMap.put("label","当前状态");
        taskNoMap.put("label","任务号");
        taskTypeMap.put("label","任务类型");
        startLocateMap.put("label","任务起始位置");
        endLocateMap.put("label","任务终点位置");
        errorCodeMap.put("label","故障报警");
        SrmInfo srmInfo = memItemMapper.getSrmInfo(deviceId);


        taskCountMap.put("value",srmInfo.getTaskCount());
//        if(srmInfo.getStatus().equals("3")){
//            statusMap.put("value", "联机");
//        }else{
//            statusMap.put("value", "脱机");
//        }
        switch (srmInfo.getStatus()){
            case "0":
                statusMap.put("value", "维修");
                break;
            case "1":
                statusMap.put("value", "手动");
                break;
            case"2":
                statusMap.put("value", "单机自动");
                break;
            default:
                statusMap.put("value", "联机");
                    break;
        }
        taskNoMap.put("value", srmInfo.getTaskNo());
        switch (srmInfo.getTaskType()){
            case "10" :
                taskTypeMap.put("value","入库" );
                break;
            case "20":
                taskTypeMap.put("value","出库" );
                break;
            case "21":
                taskTypeMap.put("value","移库" );
                break;
            default :
                taskTypeMap.put("value",srmInfo.getTaskType() );
                    break;
        }
        startLocateMap.put("value", srmInfo.getStartLocate());
        endLocateMap.put("value", srmInfo.getEndLocate());
        if(!srmInfo.getErrorCode().equals("0")){
            String errorMsg = memItemMapper.findERRMSG(srmInfo.getErrorCode()).getDescription();
            errorCodeMap.put("value", errorMsg);
        }else{
            errorCodeMap.put("value", "");
        }

        listMap.add(taskCountMap);
        listMap.add(statusMap);
        listMap.add(taskNoMap);
        listMap.add(taskTypeMap);
        listMap.add(startLocateMap);
        listMap.add(endLocateMap);
        listMap.add(errorCodeMap);

        return listMap;
    }
}
